<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maximizeGreatness = function (nums) {
      nums.sort((a, b) => a - b)
      let i = 0
      let res = 0
      for (const num of nums) {
        if (num > nums[i]) {
          i++
          res++
        }
      }
      return res
    };
    console.log(maximizeGreatness([1, 3, 5, 2, 1, 3, 1]
    ));
  </script>
</body>

</html>